//
//  BLTagView.m
//  qbo
//
//  Created by iBenz on 16/12/22.
//  Copyright © 2016年 QBO. All rights reserved.
//

#import "BLTagView.h"

@implementation BLTagView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _edgeInsets = (UIEdgeInsets){12,10,12,10};
        _lineSpacing = 12.0;
        _rowSpacing = 12.0;
    }
    return self;
}

- (void)setTags:(NSArray<BLTag *> *)tags {
    [_tags makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _tags = tags;
    for (BLTag *tag in tags) {
        if ([tag actionsForTarget:self forControlEvent:UIControlEventTouchUpInside].count==0) {
            [tag addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self addSubview:tag];
    }
    [self reloadData];
}

- (void)reloadData {
    CGSize fitSize = self.bounds.size;
    CGFloat orx = _edgeInsets.left;
    CGFloat ory = _edgeInsets.top;
    CGFloat maxWid = fitSize.width-_edgeInsets.left-_edgeInsets.right;
    CGFloat minX = _edgeInsets.left;
    CGFloat maxX = maxWid+minX;
    
    for (BLTag *tag in _tags) {
        [tag sizeToFit];
        CGRect frame = tag.frame;
        frame.origin.x = orx;
        frame.origin.y = ory;
        
        CGSize size = frame.size;//[tag intrinsicContentSize];
        if (size.width>maxWid) {
            frame.size.width = maxWid;
            if (orx>_edgeInsets.left) {
                ory += (size.height+_lineSpacing);
                orx = minX;
            }
            frame.origin.x = orx;
            frame.origin.y = ory;
        }
        else {
            if (orx+size.width>maxX) {
                ory += (size.height+_lineSpacing);
                orx = minX;
                frame.origin.x = orx;
                frame.origin.y = ory;
            }
        }
        tag.frame = frame;
        orx = CGRectGetMaxX(frame)+_rowSpacing;
    }
}

//- (CGSize)intrinsicContentSize {
//    CGSize size = CGSizeZero;
//    CGFloat maxY = 0;
//    CGFloat maxX = 0;
//    for (BLTag *tag in _tags) {
//        maxX = MAX(maxX, CGRectGetMaxX(tag.frame));
//        maxY = MAX(maxY, CGRectGetMaxY(tag.frame));
//    }
//    size.width = maxX+_edgeInsets.right;
//    size.height = maxY+_edgeInsets.bottom;
//    return size;
//}

//- (CGSize)sizeThatFits:(CGSize)fitSize {
//}

- (void)setBounds:(CGRect)bounds {
    if (!CGSizeEqualToSize(self.bounds.size, bounds.size)) {
        [super setBounds:bounds];
        [self reloadData];
    }
    else {
        [super setBounds:bounds];
    }
}

- (void)setFrame:(CGRect)frame {
    if (!CGSizeEqualToSize(self.frame.size, frame.size)) {
        [super setFrame:frame];
        [self reloadData];
    }
    else {
        [super setFrame:frame];
    }
}

- (CGSize)sizeThatFits:(CGSize)size {
    CGSize fitSize = size;
    CGFloat orx = _edgeInsets.left;
    CGFloat ory = _edgeInsets.top;
    CGFloat maxWid = fitSize.width-_edgeInsets.left-_edgeInsets.right;
    CGFloat minX = _edgeInsets.left;
    CGFloat maxX = maxWid+minX;
    
    for (BLTag *tag in _tags) {
        [tag sizeToFit];
        CGRect frame = tag.frame;
        frame.origin.x = orx;
        frame.origin.y = ory;
        
        CGSize size = frame.size;//[tag intrinsicContentSize];
        if (size.width>maxWid) {
            frame.size.width = maxWid;
            if (orx>_edgeInsets.left) {
                ory += (size.height+_lineSpacing);
                orx = minX;
            }
            frame.origin.x = orx;
            frame.origin.y = ory;
        }
        else {
            if (orx+size.width>maxX) {
                ory += (size.height+_lineSpacing);
                orx = minX;
                frame.origin.x = orx;
                frame.origin.y = ory;
            }
        }
        orx = CGRectGetMaxX(frame)+_rowSpacing;
    }
    
    CGSize result = CGSizeZero;
    result.width = maxX;
    result.height = ory+[_tags.lastObject bounds].size.height+_edgeInsets.bottom;
    return result;
}

- (void)click:(BLTag *)tag {
    tag.selected = !tag.selected;
    if (tag.selected) {
        if ([_delegate respondsToSelector:@selector(tagView:didSelectedTag:)]) {
            [_delegate tagView:self didSelectedTag:tag];
        }
    }
    else {
        if ([_delegate respondsToSelector:@selector(tagView:deselectedTag:)]) {
            [_delegate tagView:self deselectedTag:tag];
        }
    }
}

@end

@interface BLTag ()
@end
@implementation BLTag

+ (instancetype)tagWithTitle:(NSString *)title {
    BLTag *tag = [self buttonWithType:UIButtonTypeCustom];
    [tag setTitle:title forState:UIControlStateNormal];
    return tag;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _color = [UIColor whiteColor];
        _selectedColor = [UIColor grayColor];
        self.titleLabel.font = [UIFont systemFontOfSize:11.0];
        _border = [CALayer layer];
        _border.masksToBounds = YES;
        _border.borderColor = _color.CGColor;
        _border.borderWidth = 0.5;
        _border.cornerRadius = 4.0;
        _border.backgroundColor = [UIColor clearColor].CGColor;
        [self.layer insertSublayer:_border atIndex:0];
        
        self.contentEdgeInsets = (UIEdgeInsets){5,10,5,10};
        
        [self setTitleColor:_color forState:UIControlStateNormal];
        [self setTitleColor:_selectedColor forState:UIControlStateSelected];
    }
    return self;
}

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    [self fitLayer];
}
- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    [self fitLayer];
}
- (void)fitLayer {
    _border.bounds = self.bounds;
    _border.frame = self.bounds;
}
- (void)setColor:(UIColor *)color {
    _color = color;
    [self setTitleColor:_color forState:UIControlStateNormal];
    if (!self.selected) {
        self.selected = self.selected;
    }
}

- (void)setSelectedColor:(UIColor *)selectedColor {
    _selectedColor = selectedColor;
    [self setTitleColor:_selectedColor forState:UIControlStateSelected];
    if (self.selected) {
        self.selected = self.selected;
    }
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    _border.borderColor = selected?_selectedColor.CGColor:_color.CGColor;
}

- (NSString *)title {
    return [self titleForState:UIControlStateNormal];
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    _border.cornerRadius = cornerRadius;
}

@end
